[[...path]].page.tsx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import React from 'react';
  2. import { IUser, IUserHasId } from '@growi/core';
  3. import {
  4. NextPage, GetServerSideProps, GetServerSidePropsContext,
  5. } from 'next';
  6. import { serverSideTranslations } from 'next-i18next/serverSideTranslations';
  7. import dynamic from 'next/dynamic';
  8. import { ShareLinkLayout } from '~/components/Layout/ShareLinkLayout';
  9. import GrowiContextualSubNavigation from '~/components/Navbar/GrowiContextualSubNavigation';
  10. import { Page } from '~/components/Page';
  11. import { SupportedAction, SupportedActionType } from '~/interfaces/activity';
  12. import { CrowiRequest } from '~/interfaces/crowi-request';
  13. import { RendererConfig } from '~/interfaces/services/renderer';
  14. import { IShareLinkHasId } from '~/interfaces/share-link';
  15. import {
  16. useCurrentUser, useCurrentPagePath, useCurrentPathname, useCurrentPageId, useRendererConfig,
  17. useShareLinkId, useIsSearchServiceConfigured, useIsSearchServiceReachable, useIsSearchScopeChildrenAsDefault,
  18. } from '~/stores/context';
  19. import {
  20. CommonProps, getServerSideCommonProps, useCustomTitle, getNextI18NextConfig, addActivity,
  21. } from '../utils/commons';
  22. const ShareLinkAlert = dynamic(() => import('~/components/Page/ShareLinkAlert'), { ssr: false });
  23. const ForbiddenPage = dynamic(() => import('~/components/ForbiddenPage'), { ssr: false });
  24. type Props = CommonProps & {
  25. shareLink?: IShareLinkHasId,
  26. isExpired: boolean,
  27. currentUser: IUser,
  28. disableLinkSharing: boolean,
  29. isSearchServiceConfigured: boolean,
  30. isSearchServiceReachable: boolean,
  31. isSearchScopeChildrenAsDefault: boolean,
  32. rendererConfig: RendererConfig,
  33. };
  34. const SharedPage: NextPage<Props> = (props: Props) => {
  35. useShareLinkId(props.shareLink?._id);
  36. useCurrentPageId(props.shareLink?.relatedPage._id);
  37. useCurrentPagePath(props.shareLink?.relatedPage.path);
  38. useCurrentUser(props.currentUser);
  39. useCurrentPathname(props.currentPathname);
  40. useRendererConfig(props.rendererConfig);
  41. useIsSearchServiceConfigured(props.isSearchServiceConfigured);
  42. useIsSearchServiceReachable(props.isSearchServiceReachable);
  43. useIsSearchScopeChildrenAsDefault(props.isSearchScopeChildrenAsDefault);
  44. const isNotFound = props.shareLink == null || props.shareLink.relatedPage == null || props.shareLink.relatedPage.isEmpty;
  45. const isShowSharedPage = !props.disableLinkSharing && !isNotFound && !props.isExpired;
  46. return (
  47. <ShareLinkLayout title={useCustomTitle(props, 'GROWI')} expandContainer={props.isContainerFluid}>
  48. <div className="h-100 d-flex flex-column justify-content-between">
  49. <header className="py-0 position-relative">
  50. {isShowSharedPage && <GrowiContextualSubNavigation isLinkSharingDisabled={props.disableLinkSharing} />}
  51. </header>
  52. <div id="grw-fav-sticky-trigger" className="sticky-top"></div>
  53. <div className="flex-grow-1">
  54. <div id="content-main" className="content-main grw-container-convertible">
  55. { props.disableLinkSharing && (
  56. <div className="mt-4">
  57. <ForbiddenPage isLinkSharingDisabled={props.disableLinkSharing} />
  58. </div>
  59. )}
  60. { (isNotFound && !props.disableLinkSharing) && (
  61. <div className="container-lg">
  62. <h2 className="text-muted mt-4">
  63. <i className="icon-ban" aria-hidden="true" />
  64. <span> Page is not found</span>
  65. </h2>
  66. </div>
  67. )}
  68. { (props.isExpired && !props.disableLinkSharing) && (
  69. <div className="container-lg">
  70. <h2 className="text-muted mt-4">
  71. <i className="icon-ban" aria-hidden="true" />
  72. <span> Page is expired</span>
  73. </h2>
  74. </div>
  75. )}
  76. {(isShowSharedPage && props.shareLink != null) && (
  77. <>
  78. <ShareLinkAlert expiredAt={props.shareLink.expiredAt} createdAt={props.shareLink.createdAt} />
  79. <Page />
  80. </>
  81. )}
  82. </div>
  83. </div>
  84. </div>
  85. </ShareLinkLayout>
  86. );
  87. };
  88. function injectServerConfigurations(context: GetServerSidePropsContext, props: Props): void {
  89. const req: CrowiRequest = context.req as CrowiRequest;
  90. const { crowi } = req;
  91. props.disableLinkSharing = crowi.configManager.getConfig('crowi', 'security:disableLinkSharing');
  92. props.isSearchServiceConfigured = crowi.searchService.isConfigured;
  93. props.isSearchServiceReachable = crowi.searchService.isReachable;
  94. props.isSearchScopeChildrenAsDefault = crowi.configManager.getConfig('crowi', 'customize:isSearchScopeChildrenAsDefault');
  95. props.rendererConfig = {
  96. isEnabledLinebreaks: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaks'),
  97. isEnabledLinebreaksInComments: crowi.configManager.getConfig('markdown', 'markdown:isEnabledLinebreaksInComments'),
  98. adminPreferredIndentSize: crowi.configManager.getConfig('markdown', 'markdown:adminPreferredIndentSize'),
  99. isIndentSizeForced: crowi.configManager.getConfig('markdown', 'markdown:isIndentSizeForced'),
  100. plantumlUri: process.env.PLANTUML_URI ?? null,
  101. blockdiagUri: process.env.BLOCKDIAG_URI ?? null,
  102. // XSS Options
  103. isEnabledXssPrevention: crowi.configManager.getConfig('markdown', 'markdown:xss:isEnabledPrevention'),
  104. attrWhiteList: crowi.xssService.getAttrWhiteList(),
  105. tagWhiteList: crowi.xssService.getTagWhiteList(),
  106. highlightJsStyleBorder: crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  107. };
  108. }
  109. async function injectNextI18NextConfigurations(context: GetServerSidePropsContext, props: Props, namespacesRequired?: string[] | undefined): Promise<void> {
  110. const nextI18NextConfig = await getNextI18NextConfig(serverSideTranslations, context, namespacesRequired);
  111. props._nextI18Next = nextI18NextConfig._nextI18Next;
  112. }
  113. export const getServerSideProps: GetServerSideProps = async(context: GetServerSidePropsContext) => {
  114. const req = context.req as CrowiRequest<IUserHasId & any>;
  115. const { user, crowi } = req;
  116. const result = await getServerSideCommonProps(context);
  117. if (!('props' in result)) {
  118. throw new Error('invalid getSSP result');
  119. }
  120. const props: Props = result.props as Props;
  121. if (user != null) {
  122. props.currentUser = user.toObject();
  123. }
  124. const { linkId } = req.params;
  125. try {
  126. const ShareLinkModel = crowi.model('ShareLink');
  127. const shareLink = await ShareLinkModel.findOne({ _id: linkId }).populate('relatedPage');
  128. if (shareLink != null) {
  129. props.isExpired = shareLink.isExpired();
  130. props.shareLink = shareLink.toObject();
  131. }
  132. }
  133. catch (err) {
  134. //
  135. }
  136. injectServerConfigurations(context, props);
  137. // await injectUserUISettings(context, props);
  138. await injectNextI18NextConfigurations(context, props);
  139. let action: SupportedActionType;
  140. if (props.isExpired) {
  141. action = SupportedAction.ACTION_SHARE_LINK_EXPIRED_PAGE_VIEW;
  142. }
  143. else if (props.shareLink == null) {
  144. action = SupportedAction.ACTION_SHARE_LINK_NOT_FOUND;
  145. }
  146. else {
  147. action = SupportedAction.ACTION_SHARE_LINK_PAGE_VIEW;
  148. }
  149. await addActivity(context, action);
  150. return {
  151. props,
  152. };
  153. };
  154. export default SharedPage;